package com.centaline.androidsalesblog.activities;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;

import com.bumptech.glide.DrawableRequestBuilder;
import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.adapter.ImgListBrowseAdapter;
import com.centaline.androidsalesblog.base.BaseActivity;
import com.centaline.androidsalesblog.constants.ExtraConstant;
import com.centanet.framework.glide.GlideLoad;
import com.centanet.framework.service.ImageDownloadService;

import java.util.ArrayList;
import java.util.Locale;

/**
 * 租售图片列表浏览
 * Created by guilin on 16/1/31.
 */
public class ImgListBrowseActivity extends BaseActivity {

    protected ViewPager viewPager;
    protected DrawableRequestBuilder<String> requestBuilder;

    protected ImgListBrowseAdapter imgListBrowseAdapter;
    protected ArrayList<String> list = new ArrayList<>();//大图
    protected ArrayList<String> listThumbnail = new ArrayList<>();//缩略图
    protected int currentPos;//当前位置

    private ImageDownloadService.DownloadBinder downloadBinder;
    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            downloadBinder = (ImageDownloadService.DownloadBinder) service;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    @Override
    protected int setLayoutId() {
        return R.layout.activity_img_list_browse;
    }

    @Override
    protected void findViews() {
        setUniversalToolbar();
        viewPager = (ViewPager) findViewById(R.id.viewPager);
    }

    @Override
    protected void initViews() {
        requestBuilder = GlideLoad.init(this);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                pageSelected(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        imgListBrowseAdapter = new ImgListBrowseAdapter(viewPager, requestBuilder, list, listThumbnail);
        viewPager.setAdapter(imgListBrowseAdapter);

        list.clear();
        list.addAll(getIntent().getStringArrayListExtra(ExtraConstant.IMG_LIST));
        final ArrayList<String> thumbnailTemp = getIntent().getStringArrayListExtra(ExtraConstant.IMG_LIST_THUMBNAIL);
        if (thumbnailTemp != null) {
            listThumbnail.addAll(thumbnailTemp);
        }
        currentPos = getIntent().getIntExtra(ExtraConstant.IMG_POS, 0);
        imgListBrowseAdapter.notifyDataSetChanged();
        pageSelected(currentPos);
        viewPager.setCurrentItem(currentPos, true);
        bindService(new Intent(this, ImageDownloadService.class), serviceConnection, BIND_AUTO_CREATE);
    }

    protected void pageSelected(int position) {
        currentPos = position;
        setToolbarTitle(String.format(Locale.CHINA, "%d/%d", position + 1, list.size()));
    }

    @Override
    protected void onDestroy() {
        unbindService(serviceConnection);
        super.onDestroy();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (list.size() > 0)
            getMenuInflater().inflate(R.menu.menu_download, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.action_download) {
            downloadBinder.startDownload(list.get(currentPos));
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
